use std::collections::HashMap;

use glass::{reflect, Callable, FunctionWrapper, Object, Reflect, Value};

#[test]
fn reflect() {
    #[derive(Clone, Reflect)]
    struct Foo {
        name: Option<String>,
        age: i32,
    }
    let foo = Foo {
        name: Some("zhangsan".into()),
        age: 23,
    };
    let object: Object = Foo::try_into(foo.clone()).unwrap();
    let value1 = object.value;
    println!("{value1:?}");

    #[reflect]
    impl Foo {
        fn call(f: Foo) {
            println!("foo.name = {:?}", f.name);
            println!("foo.age = {}", f.age);
        }
    }

    let functions = Foo::functions();
    for function in functions {
        let ident = function.ident;
        if ident.eq("call") {
            if let Some(FunctionWrapper::Sync(function)) = function.function {
                let _ = function.call(vec![value1]);
                let _ = function.call(vec![Value::Any(Box::new(foo))]);
                break;
            }
        }
    }

    let mut fields = HashMap::new();
    fields.insert(
        String::from("name"),
        Value::Option(Some(Box::new(Value::String(String::from("zhangsan"))))),
    );
    fields.insert("age".into(), Value::I32(23));
    let value2 = Value::NamedStruct(fields);
    println!("{value2:?}");
}
